<template>
  <a-modal v-model:open="open"
           :width="960"
           :footer="null"
           style="top:10px"
           :title="formState.id?'修改部门':'添加部门'"
           :keyboard="false"
           :mask-closable="false"
           @cancel="handleClose">
    <a-form
        :model="formState"
        name="basic"
        class="mt20"
        layout="horizontal"
        style="width: 100%;"
        ref="formRef"
        :rules="rules"
        :label-col=" { style: { width: '150px' } }"
        :wrapper-col="{ span: 24}"
        autocomplete="off"
    >
      <a-row class="flex justify-center">
        <a-col :span="18">

          <a-form-item
              label="公司"
              name="systemId"
          >
            <a-input v-model:value.trim="formState.systemName" disabled maxlength="100" placeholder="请输入公司名称"/>
            <a-input v-model:value.trim="formState.systemId" type="hidden" maxlength="100" placeholder="请输入公司ID"/>
          </a-form-item>

          <a-form-item
              label="父部门"
              name="parentId"
              v-if="false"
          >
            <a-input v-model:value.trim="formState.parentId" maxlength="100" placeholder="请输入父部门"/>
          </a-form-item>

          <a-form-item
              label="部门名称"
              name="name"
          >
            <a-input v-model:value.trim="formState.name" maxlength="100" placeholder="请输入部门名称"/>
          </a-form-item>

          <a-form-item
              label="部门排序"
              name="sorted"
          >
            <a-input v-model:value.trim="formState.sorted" maxlength="100" placeholder="请输入部门排序"/>
          </a-form-item>

          <a-form-item
              label="状态"
              name="status"
          >
            <a-switch v-model:checked="formState.status"
                      :checked-value="1"
                      :un-checked-value="0"
                      checked-children="激活"
                      un-checked-children="未激活"/>
          </a-form-item>
          <a-form-item>
            <a-button style="margin-left: 150px" type="primary" @click.stop.prevent="onSubmit">
              <EditOutlined v-if="formState.id "></EditOutlined>
              <PlusOutlined v-else></PlusOutlined>
              {{ formState.id ? '更新部门' : '保存部门' }}
            </a-button>
            <a-button style="margin-left: 10px" danger type="primary" @click.stop.prevent="handleClose">
              <close-outlined></close-outlined>
              关闭
            </a-button>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>
<script setup>
const open = ref(false);
const formRef = ref({})
const emits = defineEmits(["success"])
const formState = ref({
  name:'',
  parentId:0,
  status: 1,
  sorted:1,
  systemId:0,
  systemName:""
});

// 验证规则
const rules = {
  name: [ {required: true, message: '请输入部门名称', trigger: 'blur'}],
};

const onSubmit = async () => {
  try {
    await formRef.value.validate()
    await useSaveAdminDepartmentDataApi(formState.value)
    useMessage().success("操作成功")
    handleClose()
    emits("success")
  } catch (e) {
    useMessage().error(e.msg)
  }
};

const handleSystemOpen = async (record) => {
  open.value = true
  formState.value.systemId = record.id
  formState.value.systemName = record.name
  formState.value.parentId = 0
}


const handleOpen = async (record) => {
  open.value = true
  formState.value.systemId = record.systemId
  formState.value.systemName = record.systemName
  formState.value.parentId = record.id
}


const handleEditOpen = async (record) => {
  open.value = true
  if(record && record.id){
    const resp = await useGetAdminSystemDepartmentByIdApi(record.id)
    formState.value = resp.data
  }
}

const handleClose = () => {
  open.value = false
  formRef.value.clearValidate()
  formRef.value.resetFields()
  formState.value = {
    id:"",
    name:'',
    parentId:0,
    status: 1,
    sorted:1,
    systemId:0,
    systemName:""
  }
}

defineExpose({
  handleOpen,
  handleSystemOpen,
  handleEditOpen
})
</script>

<style lang="scss" scoped>
.full-modal {
  .ant-modal {
    max-width: 100%;
    top: 0;
    padding-bottom: 0;
    margin: 0;
  }

  .ant-modal-content {
    display: flex;
    flex-direction: column;
    height: calc(100vh);
    overflow: auto;
    border-radius: 0px;
  }

  :where(.css-dev-only-do-not-override-1hsjdkk).ant-modal .ant-modal-header {
    background: transparent;
  }

  .ant-modal-body {
    flex: 1;
  }
}
</style>